package edu.ntu.cloudroid.model.friend;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


import com.google.appengine.api.datastore.Key;

import edu.ntu.cloudroid.model.GAEData;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.profile.ProfileData;
import edu.ntu.cloudroid.server.backendservice.GroupService;
import edu.ntu.cloudroid.server.backendservice.ProfileService;

/**
 * @author ttadinh
 * Represent the friend list, for each user
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@FetchGroup(name = FriendData.FETCH_GROUP_NAME, members = {
		@Persistent(name = "friendListKey"),
		@Persistent(name = "friendTimestamps") })
public class FriendData implements Serializable, GAEData{
	private static final long serialVersionUID = 1L;

	 public static final String FETCH_GROUP_NAME = "friendList";
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key; 	
	@Persistent
	private String userName;
	@Persistent
	private String friendName;
	@Persistent
	private Date friendTimestamp;
				
	public Date getFriendTimestamp() {
		return this.friendTimestamp;
	}

	public synchronized void setFriendTimestamp(Date ts) {
		this.friendTimestamp = ts;
	}

	public void setUserName(String un){
		this.userName = un;
	}
	
	public String getUserName(){
		return this.userName;
	}
	
	public void setFriendName(String fn){
		this.friendName = fn;
	}
	
	public String getFriendName(){
		return this.friendName;
	}
	
	public Key getKey() {
		return key;
	}

	public synchronized void setKey(Key key) {
		this.key = key;
	}
		
	public synchronized void updateTimestamp(Date ts) throws Exception{
		this.setFriendTimestamp(ts);
	}

	@Override
	public UserData getMetaData(Object param) throws Exception{
		/*List<String> list = new ArrayList<String>();
		ProfileData p = ProfileService.getProfile(this.userName);
		List<GroupData> groups = GroupService.getGroupFromProfile(p);
		for (Key k: this.friendList){
			list.add(ProfileService.getProfile(k).getEmailAddress());
		}
		
		List<String> groupList = new ArrayList<String>();
		for (GroupData gd : groups){
			groupList.add(gd.getName());
		}*/
		
		return new FriendInfo(this.userName, this.friendName, FriendInfo.REPLY);
	}
}
